﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VOL.Core.Extensions;
using VOL.Domain.Infrastructure;
using VOL.Domain.IServices.WarehouseManagement;
using VOL.Entity.DomainModels;

namespace VOL.Domain.Services.WarehouseManagement
{
    /// <summary>
    /// 外协入库
    /// </summary>
    public class OutsourcingWarehousingServices : IOutsourcingWarehousingServices
    {
        private readonly IBaseRepository<OutsourcingWarehousing> _outsourcingWarehousingRepository;

        public OutsourcingWarehousingServices(IBaseRepository<OutsourcingWarehousing> outsourcingWarehousingRepository)
        {
            _outsourcingWarehousingRepository = outsourcingWarehousingRepository;
        }
        /// <summary>
        /// 外协入库新增
        /// </summary>
        /// <param name="outsourcingWarehousing"></param>
        /// <returns></returns>
        public async Task<int> AddOutsourcingWarehousing(OutsourcingWarehousing outsourcingWarehousing)
        {
            return await _outsourcingWarehousingRepository.Add(outsourcingWarehousing);
        }
        /// <summary>
        /// 外协入库删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteOutsourcingWarehousing(int id)
        {
            var outsourcingWarehousing = (await _outsourcingWarehousingRepository.FindAsync(m => m.Id == id)).FirstOrDefault();
            if (outsourcingWarehousing == null) return 0;
            return await _outsourcingWarehousingRepository.Delete(outsourcingWarehousing);
        }
        /// <summary>
        /// 外协入库显示
        /// </summary>
        /// <returns></returns>
        public async Task<List<OutsourcingWarehousing>> GetAllOutsourcingWarehousing(string? stockroomCode, string? outsourcing, string? supplierName, int pageIndex = 1, int pageSize = 5)
        {
            var outsourcingWarehousingList = _outsourcingWarehousingRepository.GetAll();
            outsourcingWarehousingList = outsourcingWarehousingList.WhereIF(stockroomCode != null, m => m.StockroomCode.Contains(stockroomCode));
            outsourcingWarehousingList = outsourcingWarehousingList.WhereIF(outsourcing != null, m => m.Outsourcing.Contains(outsourcing));
            outsourcingWarehousingList = outsourcingWarehousingList.WhereIF(supplierName != null, m => m.SupplierName.Contains(supplierName));
            var total = outsourcingWarehousingList.Count();
            var items = outsourcingWarehousingList.OrderBy(m => m.Id).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return  outsourcingWarehousingList.ToList();
        }
        /// <summary>
        /// 外协入库修改
        /// </summary>
        /// <param name="outsourcingWarehousing"></param>
        /// <returns></returns>
        public async Task<int> UpdateOutsourcingWarehousing(OutsourcingWarehousing outsourcingWarehousing)
        {
            return await _outsourcingWarehousingRepository.Update(outsourcingWarehousing);
        }
    }
}
